package com.shicijiaoliu.service.impl;

import com.github.pagehelper.PageHelper;
import com.shicijiaoliu.mapper.Mainpostmapper;
import com.shicijiaoliu.pojo.Mainpost;
import com.shicijiaoliu.service.Mainpostservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;

@Service
public class Mainpostserviceimpl implements Mainpostservice {
    @Autowired
    Mainpostmapper mainpostmapper;

    @Override
    public int getcount(String title, long aId, long sId) {
        return mainpostmapper.getcount(title,aId,sId);
    }


    @Override
    public List<Mainpost> findBypage(int pagecount, int pagenumber, String title, long aId, long sId) {
        int count = mainpostmapper.getcount(null,0,0);
        int totalpage = (int) Math.ceil((double)count/pagenumber);
        if(pagecount>totalpage || pagecount <= 0){
            return null;
        }else{
            PageHelper.startPage(pagecount,pagenumber);
            return mainpostmapper.findBypage(title,aId,sId);
        }
    }



    @Override
    public void delByid(long id) {
        mainpostmapper.delByid(id);
    }

    @Override
    public Mainpost findByid(long id) {
        return mainpostmapper.findByid(id);
    }

    @Override
    public Mainpost findBytitle(String title) {
        return mainpostmapper.findBytitle(title);
    }

    @Override
    public void jiajing(long mId) {
        String isjing = "是";
        if(mainpostmapper.findByid(mId).getIsjing().equals(isjing))
            isjing = "否";
        mainpostmapper.jiajing(mId,isjing);
    }

    @Override
    public List<Mainpost> getlist() {
        return mainpostmapper.getlist();
    }

    @Override
    public List<Mainpost> findBysid(long sid) {
        return mainpostmapper.findBysid(sid);
    }

    @Override
    public void update(Mainpost mpost) {
        mpost.setFinaltime(LocalDateTime.now());
        mainpostmapper.update(mpost);
    }

    @Override
    public void jubao(long mId) {
        String jubao = "是";
        if(mainpostmapper.findByid(mId).getJubao().equals(jubao))
            jubao = "否";
        mainpostmapper.jubao(mId,jubao);
    }

    @Override
    public void add(Mainpost mpost) {
        mainpostmapper.add(mpost);
    }
}
